package Tudy

import (
	"fmt"
	"strings"
)

var ActionMap map[string]map[string]string
var GoToMap map[string]map[string]string
var ActionString =[]string{"i","+","*","(",")","#"}
var GoToString=[]string{"E","T","F"}
var Grammer map[string]string
var LengthGramer map[string]string
func init(){
	ActionMap=make(map[string]map[string]string)
	ActionMap["0"]=make(map[string]string)
	ActionMap["0"]["i"]="S5"
	ActionMap["0"]["("]="S4"
	ActionMap["1"]=make(map[string]string)
	ActionMap["1"]["+"]="S6"
	ActionMap["1"]["#"]="acc"
	ActionMap["2"]=make(map[string]string)
	ActionMap["2"]["+"]="r2"
	ActionMap["2"]["*"]="S7"
	ActionMap["2"][")"]="r2"
	ActionMap["2"]["#"]="r2"
	ActionMap["3"]=make(map[string]string)
	ActionMap["3"]["+"]="r4"
	ActionMap["3"]["*"]="r4"
	ActionMap["3"][")"]="r4"
	ActionMap["3"]["#"]="r4"
	ActionMap["4"]=make(map[string]string)
	ActionMap["4"]["i"]="S5"
	ActionMap["4"]["("]="S4"
	ActionMap["5"]=make(map[string]string)
	ActionMap["5"]["+"]="r6"
	ActionMap["5"]["*"]="r6"
	ActionMap["5"][")"]="r6"
	ActionMap["5"]["#"]="r6"
	ActionMap["6"]=make(map[string]string)
	ActionMap["6"]["i"]="S5"
	ActionMap["6"]["("]="S4"
	ActionMap["7"]=make(map[string]string)
	ActionMap["7"]["i"]="S5"
	ActionMap["7"]["("]="S4"
	ActionMap["8"]=make(map[string]string)
	ActionMap["8"]["+"]="S6"
	ActionMap["8"][")"]="S11"
	ActionMap["9"]=make(map[string]string)
	ActionMap["9"]["+"]="r1"
	ActionMap["9"]["*"]="S7"
	ActionMap["9"][")"]="r1"
	ActionMap["9"]["#"]="r1"
	ActionMap["10"]=make(map[string]string)
	ActionMap["10"]["+"]="r3"
	ActionMap["10"]["*"]="r3"
	ActionMap["10"][")"]="r3"
	ActionMap["10"]["#"]="r3"
	ActionMap["11"]=make(map[string]string)
	ActionMap["11"]["+"]="r5"
	ActionMap["11"]["*"]="r5"
	ActionMap["11"][")"]="r5"
	ActionMap["11"]["#"]="r5"
	GoToMap=make(map[string]map[string]string)
	GoToMap["0"]=make(map[string]string)
	GoToMap["0"]["E"]="1"
	GoToMap["0"]["T"]="2"
	GoToMap["0"]["F"]="3"
	GoToMap["1"]=make(map[string]string)
	GoToMap["2"]=make(map[string]string)
	GoToMap["3"]=make(map[string]string)
	GoToMap["4"]=make(map[string]string)
	GoToMap["4"]["E"]="8"
	GoToMap["4"]["T"]="2"
	GoToMap["4"]["F"]="3"
	GoToMap["5"]=make(map[string]string)
	GoToMap["6"]=make(map[string]string)
	GoToMap["6"]["T"]="9"
	GoToMap["6"]["F"]="3"
	GoToMap["7"]=make(map[string]string)
	GoToMap["7"]["F"]="10"
	GoToMap["8"]=make(map[string]string)
	GoToMap["9"]=make(map[string]string)
	GoToMap["10"]=make(map[string]string)
	GoToMap["11"]=make(map[string]string)
	Grammer=make(map[string]string)
	Grammer["E+T"]="E"
	Grammer["T"]="E"
	Grammer["T*F"]="T"
	Grammer["F"]="T"
	Grammer["(E)"]="F"
	Grammer["i"]="F"
	LengthGramer=make(map[string]string)
	LengthGramer["1"]="E+T"
	LengthGramer["2"]="T"
	LengthGramer["3"]="T*F"
	LengthGramer["4"]="F"
	LengthGramer["5"]="(E)"
	LengthGramer["6"]="i"
}
func main4() {
	fmt.Println("请输入句子")
	var str string
	fmt.Scan(&str)
	str+="#"
	StateStack:=make([]string,0)
	StateStack=append(StateStack,"0")
	SymbolStack:=make([]string,0)
	SymbolStack=append(SymbolStack,"#")
	InputStack:=make([]string,0)
	for i:=0;i<len(str);i++{
		InputStack=append(InputStack,string(str[i]))
	}
	ActionStack:=make([]string,0)
	GoTostack:=make([]string,0)
	results:=make([]string,0)
	resultActionGoto:=make([]string,0)
	for{
		ActionNext:=ActionMap[StateStack[len(StateStack)-1]][InputStack[0]]
		ActionStack=append(ActionStack,ActionNext)
		if string(ActionNext[0])=="S"{
			NextNum:=ActionNext[1:]
			StateStack=append(StateStack,NextNum)
			SymbolStack=append(SymbolStack,InputStack[0])
			InputStack=InputStack[1:]
			GoTostack=append(GoTostack,"")
		}else if string(ActionNext[0])=="r"{
			NextPop:=ActionNext[1:]
			PopString:=LengthGramer[NextPop]//b
			ChangeString:=Grammer[PopString]//A
			StateStack=StateStack[0:len(StateStack)-len(PopString)]
			SymbolStack=SymbolStack[0:len(SymbolStack)-len(PopString)]
			SymbolStack=append(SymbolStack,ChangeString)
			PopNum:=StateStack[len(StateStack)-1]
			NextNum:=GoToMap[PopNum][ChangeString]
			StateStack=append(StateStack,NextNum)
			GoTostack=append(GoTostack,NextNum)
		}
		results=append(results,strings.Join(StateStack,"")+"     "+strings.Join(SymbolStack,"")+"     "+strings.Join(InputStack,""))
		resultActionGoto=append(resultActionGoto,ActionStack[len(ActionStack)-1]+" "+GoTostack[len(GoTostack)-1])
		if ActionStack[len(ActionStack)-1]=="acc"{
			break
		}
	}
	fmt.Println("状态栈","符号栈","输入串","Action","GoTo")
	fmt.Println("0","  #",str,resultActionGoto[0])
	for i:=0;i<len(results)-1;i++{
		fmt.Println(results[i],"  ",resultActionGoto[i+1])
	}
}
